package com.example.understand_compose1.testcase_activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetSharedPreferenceDataStoreBinding

class FetSharedPreferenceDataStoreActivity : AppCompatActivity() {
    lateinit var binding: ActivityFetSharedPreferenceDataStoreBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetSharedPreferenceDataStoreBinding.inflate(layoutInflater)

        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

    }

    fun spwrite(view: View) {
        getSharedPreferences("sp", MODE_PRIVATE).edit()
            .putInt("age", 19)
            .putString("key1", "Hello world").apply()
        Toast.makeText(this, "sp 写入 key1: Hello world, age: 19", Toast.LENGTH_SHORT).show()
    }

    fun spread(view: View) {
        val age = getSharedPreferences("sp", MODE_PRIVATE).getInt("age", 0)
        val key1 = getSharedPreferences("sp", MODE_PRIVATE).getString("key1", "default")
        Toast.makeText(this, "sp 读出 age: $age, key1: $key1", Toast.LENGTH_SHORT).show()

    }
}